import android.graphics.Color
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.idlefish.GoodsInfo.pojo.Class_t
import com.example.idlefish.GoodsInfo.pojo.Classification
import com.example.idlefish.R

class NavAdapter(
    private val items: List<Class_t>, // 数据源
    private val onItemClick: (Int) -> Unit, // 点击事件回调
    private val recyclerView: RecyclerView
) : RecyclerView.Adapter<NavAdapter.NavViewHolder>() {

    private var selectedPosition: Int = 0

    // 自定义 ViewHolder
    class NavViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvchoice: TextView = itemView.findViewById(R.id.tvchoice) // 假设布局中有一个 TextView
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NavViewHolder {
        // 加载子项布局
        val view = LayoutInflater.from(parent.context).inflate(R.layout.home_classfication, parent, false)
        val viewHolder = NavViewHolder(view)

        return viewHolder
    }

    override fun onBindViewHolder(holder: NavViewHolder, position: Int) {
        val item = items[position]
        holder.tvchoice.text = item.name
        if (position == selectedPosition) {
            holder.tvchoice.setTypeface(null, Typeface.BOLD) // 加粗
            holder.tvchoice.setTextColor(Color.BLACK)
        } else {
            holder.tvchoice.setTypeface(null, Typeface.NORMAL) // 恢复正常
            holder.tvchoice.setTextColor(Color.GRAY)
        }
        holder.tvchoice.setOnClickListener{

            val position = holder.adapterPosition
            if (position != RecyclerView.NO_POSITION) {
                // 更新选中项位置
                selectedPosition = position
                if (recyclerView.layoutManager !is LinearLayoutManager ||
                    (recyclerView.layoutManager as LinearLayoutManager).orientation != RecyclerView.HORIZONTAL) {
                    recyclerView.layoutManager = LinearLayoutManager(
                        recyclerView.context,
                        RecyclerView.HORIZONTAL,
                        false
                    )
                }

                scrollToCenter(position)
                notifyDataSetChanged() // 刷新整个列表

                onItemClick(position)
            }
        }
        holder.itemView.setOnClickListener { onItemClick(position) } // 绑定点击事件
    }

    override fun getItemCount(): Int = items.size // 返回数据源大小
    private fun scrollToCenter(position: Int) {
        val layoutManager = recyclerView.layoutManager as? LinearLayoutManager
            ?: return

        recyclerView.post {
            val targetView = layoutManager.findViewByPosition(position)
            val recyclerViewWidth = recyclerView.width

            if (targetView != null) {
                val itemWidth = targetView.width
                val offset = (recyclerViewWidth / 2) - (itemWidth / 2)
                layoutManager.scrollToPositionWithOffset(position, offset)
            } else {
                recyclerView.smoothScrollToPosition(position)
                recyclerView.post {
                    val newTargetView = layoutManager.findViewByPosition(position)
                    if (newTargetView != null) {
                        val itemWidth = newTargetView.width
                        val offset = (recyclerViewWidth / 2) - (itemWidth / 2)
                        layoutManager.scrollToPositionWithOffset(position, offset)
                    }
                }
            }
        }
    }

}
